<?php defined('SYSPATH') or die('No direct access allowed.');

class Date extends Kohana_Date {
	
	public static function get_time_difference($start, $end) {
		$uts['start'] = strtotime($start);
		$uts['end'] = strtotime($end);
		if ($uts['start'] !== -1 && $uts['end'] !== -1) {
			if ($uts['end'] >= $uts['start']) {
				$diff = $uts['end'] - $uts['start'];
			} else {
				$diff = $uts['start'] - $uts['end'];
			}
			if (!false != ($days = intval((floor($diff / 86400))))) {
				$diff = $diff % 86400;
			}
			if (!false != ($hours = intval((floor($diff / 3600))))) {
				$diff = $diff % 3600;
			}
			if (!false != ($minutes = intval((floor($diff / 60))))) {
				$diff = $diff % 60;
			}
			$diff = intval($diff);
			return(array('days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $diff));
		} else {
			throw new Exception(__("Invalid date/time data detected"));
		}
		return(false);
	}
    
	public static function get_sql_datetime($datetime){
		if(10 == strlen($datetime)) $datetime .= " 00:00:00";
		if(13 == strlen($datetime)) $datetime .= ":00:00";
		if(16 == strlen($datetime)) $datetime .= ":00";
		return DateTime::createFromFormat("d/m/Y H:i:s", $datetime)->format("Y-m-d H:i:s");
	}
	
	public static function get_clock($end) {
		$uts['start'] = strtotime(date("Y-m-d H:i:s"));
        $uts['end'] = strtotime($end);
        if ($uts['start'] !== -1 && $uts['end'] !== -1) {
            if ($uts['end'] >= $uts['start']) {
                $diff = $uts['end'] - $uts['start'];
            } else {
                $diff = $uts['start'] - $uts['end'];
            }
            $days = intval((floor($diff / 86400)));
            $rest_days = $diff % 86400;
            $hours = intval((floor($rest_days / 3600)));
            $rest_hours = $diff % 3600;
            $minutes = intval((floor($rest_hours / 60)));
            $rest_minuts = $diff % 60;
            $clock = array(
                'days' => $days, 
                'hours' => $hours, 
                'minutes' => $minutes, 
                'seconds' => $rest_minuts,
            );
            return $clock;
        } else {
            throw new Exception(__("Invalid date/time data detected"));
        }
        return(false);
	}
    
    public static function get_array($date) {
        return getdate(strtotime($date));
    }
	
} // End Date